package com.fdb.efp.esb.service.impl.hj;

import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.BizException;
import com.fdb.efp.esb.service.bo.req.hj.ReqLoanBalanceBean;
import com.fdb.efp.esb.service.bo.req.hj.ReqLoanInfoQueryBean;
import com.fdb.efp.esb.service.bo.req.hj.ReqRepaymentDetailsInfoQueryBean;
import com.fdb.efp.esb.service.bo.resp.hj.RespLoanBalanceQueryBean;
import com.fdb.efp.esb.service.bo.resp.hj.RespLoanInfoQueryBean;
import com.fdb.efp.esb.service.bo.resp.hj.RespRepaymentPlanListQueryBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.hj.LoanInfoQueryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import static com.fdb.basic.framework.core.constant.ESBConstant.*;

/**
 * 需求编号：【2019D0519】（业务提的需求编号）
 * 问题编号：【xxxxxxx】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：chenxuan
 * 创建日期：2019/5/9
 * 功能描述：根据借据号、发放号查询借据信息开始
 */
@Service("loanInfoQueryService")
public class LoanInfoQueryServiceImpl implements LoanInfoQueryService {

    Logger logger = LoggerFactory.getLogger(LoanInfoQueryServiceImpl.class);

    /**
     * 开发人员：【chenxuan】
     * 创建/修改日期：【2019/5/9】
     * 描述：查询借据信息
     *
     * @return RespLoanInfoQueryBean
     * @Param [reqLoanInfoQueryBean]
     **/
    @Override
    public RespLoanInfoQueryBean loanInfoQuery(ReqLoanInfoQueryBean reqLoanInfoQueryBean, String applySeq) throws Exception {
        RespLoanInfoQueryBean respLoanInfoQueryBean = null;
        logger.info("======>调用贷款借据信息查询接口【" + applySeq + "_30230003_04】开始<======");
        String retMsg = "";
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(applySeq).build();

            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(reqLoanInfoQueryBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230003)
                    .withScene(ESBConstant.SCENE_NO_04)
                    .create()
                    .sendAndReceive();
            String retCd = recvBean.getSysHead().getRetInfArry().get(0).getRetCd();
            retMsg = recvBean.getSysHead().getRetInfArry().get(0).getRetMsg();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(retCd)) {
                respLoanInfoQueryBean = recvBean.getBody(RespLoanInfoQueryBean.class);
            }
            if (null != respLoanInfoQueryBean) {
                if (!StringUtils.isEmpty(respLoanInfoQueryBean.getLoanNo())) {
                    logger.info("借据号：" + reqLoanInfoQueryBean.getLoanNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo() + "借据信息查询成功");
                } else {
                    logger.info("借据号：" + reqLoanInfoQueryBean.getLoanNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo() + "对应借据信息不存在");
                    throw new BizException("查询借据信息" + TRADE_CODE_30230003 + SCENE_NO_04 + "接口查询失败！" + " 借据号：" + reqLoanInfoQueryBean.getLoanNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo());
                }
            } else {
                throw new BizException("贷款借据信息查询失败！"+retMsg);
            }
        } catch (Exception e) {
            logger.info("贷款借据信息查询失败！"+retMsg);
            throw e;
        } finally {
            logger.info("======>调用贷款借据信息查询接口【" + applySeq + "_30230003_04】结束<======");
        }
        return respLoanInfoQueryBean;
    }

    /**
     * 开发人员：【chenxuan】
     * 创建/修改日期：【2019/5/9】
     * 描述：查询还款计划
     *
     * @return RespRepaymentPlanListQueryBean
     * @Param [reqLoanInfoQueryBean]
     **/
    @Override
    public RespRepaymentPlanListQueryBean repaymentPlanListQuery(ReqRepaymentDetailsInfoQueryBean reqLoanInfoQueryBean, String applySeq) throws Exception {
        RespRepaymentPlanListQueryBean respRepaymentPlanListQueryBean = null;
        logger.info("======>调用还款计划查询接口【" + applySeq + "_30230002_02】开始<======");
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(applySeq).build();

            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(reqLoanInfoQueryBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230002)
                    .withScene(ESBConstant.SCENE_NO_02)
                    .create()
                    .sendAndReceive();
            String retCd = recvBean.getSysHead().getRetInfArry().get(0).getRetCd();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(retCd)) {
                respRepaymentPlanListQueryBean = recvBean.getBody(RespRepaymentPlanListQueryBean.class);
            }
            if (null != respRepaymentPlanListQueryBean) {
                if (respRepaymentPlanListQueryBean.getAcctDtlInfArry() != null) {
                    if (respRepaymentPlanListQueryBean.getAcctDtlInfArry().size() > 0) {
                        logger.info("贷款号：" + reqLoanInfoQueryBean.getAcctNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo() + "还款计划信息查询成功");
                    }
                } else {
                    logger.info("借据号：" + reqLoanInfoQueryBean.getAcctNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo() + "对应还款计划信息不存在");
                    throw new BizException("查询还款计划明细信息" + TRADE_CODE_30230002 + SCENE_NO_02 + "接口查询失败！" + " 借据号：" + reqLoanInfoQueryBean.getAcctNo() + " 发放号：" + reqLoanInfoQueryBean.getDistrNo());
                }
            } else {
                throw new BizException("还款计划明细信息查询失败！");
            }
        } catch (Exception e) {
            logger.info("还款计划明细信息信息查询失败！");
            throw e;
        } finally {
            logger.info("======>调用还款计划查询接口【" + applySeq + "_30230002_02】结束<======");
        }
        return respRepaymentPlanListQueryBean;
    }

    /**
     * 开发人员：【chenxuan】
     * 创建/修改日期：【2019/5/18】
     * 描述：贷款余额查询
     *
     * @return RespLoanBalanceQueryBean
     * @Param [reqLoanInfoQueryBean]
     **/
    @Override
    public RespLoanBalanceQueryBean loanBalanceQuery(ReqLoanBalanceBean reqLoanBalanceBean, String applySeq) throws Exception {
        RespLoanBalanceQueryBean respLoanBalanceQueryBean = null;
        logger.info("======>调用贷款余额查询接口【" + applySeq + "_30230003_08】开始<======");
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(applySeq).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(reqLoanBalanceBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230003)
                    .withScene(ESBConstant.SCENE_NO_08)
                    .create()
                    .sendAndReceive();
            String retCd = recvBean.getSysHead().getRetInfArry().get(0).getRetCd();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(retCd)) {
                respLoanBalanceQueryBean = recvBean.getBody(RespLoanBalanceQueryBean.class);
            }
            if (null != respLoanBalanceQueryBean) {
                if (respLoanBalanceQueryBean.getLoanNo() != null) {
                    logger.info("贷款号：" + respLoanBalanceQueryBean.getLoanNo() + "贷款余额查询查询成功");
                } else {
                    logger.info("贷款号：" + reqLoanBalanceBean.getLoanNo() + " 发放号：" + reqLoanBalanceBean.getDistrNo() + "对应还款计划信息不存在");
                    throw new BizException("（贷款余额查询）" + TRADE_CODE_30230003 + SCENE_NO_08 + "接口查询失败！" + " 贷款号：" + reqLoanBalanceBean.getLoanNo());
                }
            } else {
                throw new BizException("贷款余额查询失败！");
            }
        } catch (Exception e) {
            logger.info("贷款余额查询失败！");
            throw e;
        } finally {
            logger.info("======>调用贷款余额查询接口【" + applySeq + "_30230003_08】结束<======");
        }
        return respLoanBalanceQueryBean;
    }

}
